<style lang="less">
@import '../../../styles/common.less';
</style>
<template>
  <Card>
    <Card>
      <Button type="primary" @click="saveDate">保存</Button>
    </Card>
    <Card class="main">
      <Row align="middle" type="flex">
        <Col span="2" order="1">客户类型:</Col>
        <Col span="4" order="2">
        <Input v-model="riskParamsData.custType" :disabled="true" style="width: 112px" />
        </Col>
        <Col span="2" order="3">产品类型:</Col>
        <Col span="4" order="4">
        <Input v-model="riskParamsData.productTypeName" style="width: 112px" :disabled="true" />
        </Col>

        <Col span="2" order="5">申请金额:</Col>
        <Col span="4" order="6">
        <Input v-model="riskParamsData.applyAmount" :disabled="true" style="width: 112px" />
        </Col>

        <Col span="2" order="7">资金来源:</Col>
        <Col span="4" order="8">
        <Select v-model="riskParamsData.fundSourceCode" v-if="riskParamsData.patternType === 'B'" style="v-select-wrapper" placeholder="资金来源" @on-change="fundSourceChange" :label-in-value="true">
          <Option v-for="item in fundSourceList" :value="item.fundCode" :key=" item.fundCode">{{ item.fundName}}</Option>
        </Select>
        <Input v-model="riskParamsData.fundSourceName" :disabled="true" style="width: 112px" v-else/>
        </Col>
      </Row>
      </br>
      <Row align="middle" type="flex">

        <Col span="2" >模型认定等级:</Col>
        <Col span="4" >
        <Input v-model="riskParamsData.confirmLevel" :disabled="true" style="width: 112px" />
        </Col>
        <Col span="2">认定客户类型:</Col>
        <Col span="4"  >
        <Select :disabled="taskParams.type!=1 || riskParamsData.patternType === 'A'" style="width: 112px" v-model="riskParamsData.confirmCustType" placeholder="认定客户类型" @on-change="chooseCustomerType">
          <Option v-for="item in confirmCustTypes" :value="item.customerType" :key=" item.customerType">{{ item.customerTypeDesc}}</Option>
        </Select>
        </Col>

        <Col span="2" >核定金额:</Col>
        <Col span="4" >
        <Input :disabled="taskParams.type!=1" v-model="riskParamsData.approvedAmount" style="width: 112px" @on-blur="approvedAmountModify" />
        </Col>
        <Col span="4"  v-if="riskParamsData.productType!=='13'">
        <Button type="primary" @click="trialApprovedAmount" style="margin-top: -5px;">试算核定金额</Button>
        </Col>

      </Row>
      </br>
      <Row align="middle" type="flex">
        <Col span="2" order="1">决策金额:</Col>
        <Col span="4" order="2"><Input v-model="riskParamsData.decisionMoney" :disabled="true" style="width: 112px" /></Col>
        <Col span="2" order="3">产品小类:</Col>
        <Col span="4" order="4">
        <Input v-model="riskParamsData.productName" style="width: 112px" :disabled="true" />
        </Col>
      </Row>
      </br>
      <Row align="middle" type="flex">

        <Col span="2" order="1">月利率(%):</Col>
        <Col span="4" order="2">
        <Input v-model="riskParamsData.aprMonth" :disabled="true" style="width: 112px" />
        </Col>

        <Col span="2" order="3">咨询费率(%):</Col>
        <Col span="4" order="4">
        <Input v-model="riskParamsData.consultingFeeApr" style="width: 112px" :disabled="true" />
        </Col>
        <Col span="2" order="7">管理费率(%):</Col>
        <Col span="4" order="8">
        <Input v-model="riskParamsData.manageFeeApr" style="width: 112px" :disabled="true" />
        </Col>
        <Col span="2" order="9">年利率(%):</Col>
        <Col span="4" order="10">
        <Input v-model="riskParamsData.aprYear" style="width: 112px" :disabled="true" />
        </Col>
      </Row>
      </br>
      <Row align="middle" type="flex">

        <Col span="2" order="1">到账金额:</Col>
        <Col span="4" order="2">
        <Input v-model="riskParamsData.accountAmount" style="width: 112px" :disabled="true" />
        </Col>

        <Col span="2" order="3">小额消费贷款余额:</Col>
        <Col span="4" order="4">
        <Input v-model="riskParamsData.microfinanceLoanAmount" style="width: 112px" :disabled="true" />
        </Col>
        <Col span="2" order="5">信用卡使用率(%):</Col>
        <Col span="4" order="6">
        <Input v-model="riskParamsData.creditCardUsageRate" v-if="riskParamsData.creditCardUsageRate>'85' " style="width: 112px;color: red" :disabled="true" />
        <Input v-model="riskParamsData.creditCardUsageRate" style="width: 112px" :disabled="true" v-else />
        </Col>
        <Col span="2" order="7">系统认定收入:</Col>
        <Col span="4" order="8">
        <Input v-model="riskParamsData.systemIdentificeIncome" style="width: 112px" :disabled="true" />
        </Col>
      </Row>
      </br>
      <Row align="middle" type="flex">
        <Col span="2" order="1">首次贷款日期:</Col>
        <Col span="4" order="2">
        <Input v-model="riskParamsData.firstLoanTime" style="width: 112px" :disabled="true" />
        </Col>
        <Col span="2" order="3">首张信用卡申请日期:</Col>
        <Col span="4" order="4">
        <Input v-model="riskParamsData.firstCreditCardMaturityDate" style="width: 112px" :disabled="true" />
        </Col>
        <Col span="2" order="5">最近6个月月还款额率(%):</Col>
        <Col span="4" order="6">
        <Input v-model="riskParamsData.lastSixMonthRepaymentRate" v-if="riskParamsData.lastSixMonthRepaymentRate>'6'" style="width: 112px;color:red" :disabled="true" />
        <Input v-model="riskParamsData.lastSixMonthRepaymentRate" style="width: 112px" :disabled="true" v-else />
        </Col>
        <Col span="2" order="7">贷款期限:</Col>
        <Col span="4" order="8">
        <Input v-model="riskParamsData.loanTerms" style="width: 112px" :disabled="true" />
        </Col>
      </Row>
      </br>
      <Row align="middle" type="flex">
        <Col span="2" order="1">认定负债:</Col>
        <Col span="4" order="2">
        <Input :disabled="taskParams.type!=1" v-model="riskParamsData.identificationOfLiabilities" style="width: 112px" @on-blur="changeInput" />
        </Col>

        <Col span="2" order="3">本笔负债:</Col>
        <Col span="4" order="4">
        <Input v-model="riskParamsData.currentLiabilities" style="width: 112px" :disabled="true" @on-blur="changeInput" />
        </Col>

        <Col span="2" order="5">审批认定收入:</Col>
        <Col span="4" order="6">
        <Input :disabled="taskParams.type!=1" v-model="riskParamsData.approvalConfirmationIncome" style="width: 112px" @on-blur="changeInput" />
        </Col>
        <Col span="2" order="7">总收入负债比(%):</Col>
        <Col span="4" order="8">
        <Input v-model="riskParamsData.incomeLiabilitiesRatio" style="width: 112px" :disabled="true" />
        </Col>
        <!-- <div style="float:left;margin-left: 13px;width:250px;margin-top:40px">
                    <div style="float:left;text-align: center;width: 112px;">总收入负债比(%):</div>
                    <div>
                        <Input v-model="riskParamsData.incomeLiabilitiesRatio"  style="width: 112px" :disabled="true"/>
                    </div>
            </div> -->
      </Row>
      <div style="margin-top:60px" v-if="riskParamsData.jobsType==1">
          <div style="text-align: center;width: 112px">特殊情况说明:</div>
          <div>
            <Input :rows="4" v-model="riskParamsData.specialRemark" type="textarea" placeholder="Enter something..." />
          </div>
        </div>
    </Card>
  </Card>

</template>

<script>

export default {
  components: {

  },
  name: 'risk-params-form',
  data () {
    return {
      riskParamsData: {
        custType: '',
        custTypeCode: '',
        productTypeName: '',
        productType: '',
        applyAmount: '',
        fundSourceCode: '',
        chooseFundCode: '',
        fundSourceName: '',
        patternType: '',
        confirmLevel: '',
        confirmCustType: '',
        minLimit: '',
        maxLimit: '',
        approvedAmount: 0,
        aprMonth: '',
        consultingFeeApr: '',
        manageFeeApr: '',
        aprYear: '',
        accountAmount: '',
        decisionMoney: null,
        microfinanceLoanAmount: '',
        creditCardUsageRate: '',
        systemIdentificeIncome: '',
        firstLoanTime: '',
        firstCreditCardMaturityDate: '',
        lastSixMonthRepaymentRate: '',
        loanTerms: '',
        identificationOfLiabilities: 0,
        currentLiabilities: 0,
        approvalConfirmationIncome: 0,
        incomeLiabilitiesRatio: '',
        productCode: null,
        productName: '',
        specialRemark: null, // 特殊情况说明
        isTrialApproved: 'N', // 是否进行了核电金额试算
        // finalTrialed:'N',
        radioDebt: '', // 计算出来的负债
        incomeRedoRatio: ''
      },
      fundSourceList: [],
      confirmCustTypes: []
    }
  },
  methods: {
    initpage () {
      var vm = this
      vm.riskParamsData.loanId = this.taskParams.curLoanId
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/loanapplycase/service/getRiskParamsInfo', this.riskParamsData).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code === 'success') {
          // console.log(data.data)
          vm.riskParamsData = data.data
          vm.riskParamsData.specialRemark = data.data.specialRemark
          if (data.data != null) {
            if (vm.riskParamsData.patternType === 'B') {
              // vm.fundSourceList.push({ 'fundCode': '', 'fundName': '请选择' })
              for (let i = 0; i < data.data.fundSourceList.length; i++) {
                vm.fundSourceList.push(data.data.fundSourceList[i])
              }
            }
            // vm.confirmCustTypes.push({ 'customerType': '', 'customerTypeDesc': '请选择' })
            // console.log(data.data.confirmCustTypes)
            for (let i = 0; i < data.data.confirmCustTypes.length; i++) {
              vm.confirmCustTypes.push(data.data.confirmCustTypes[i])
            }
          }
        } else {
          this.$Message.warning({
            content: message,
            duration: 0,
            closable: true
          })
        }
      }).catch(er => {

      })
    },
    chooseCustomerType () {
      var vm = this
      // console.log(this.riskParamsData)
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/loanapplycase/service/payPremiumAnnualPayment', this.riskParamsData).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code === 'success') {
          vm.riskParamsData.systemIdentificeIncome = data.data.systemIdentificeIncome
          vm.riskParamsData.productCode = data.data.productCode
          vm.riskParamsData.productName = data.data.productName
          vm.riskParamsData.fundSourceCode = data.data.fundSourceCode
          vm.fundSourceList = data.data.fundSourceList
          vm.riskParamsData.currentLiabilities = data.data.currentLiabilities
          vm.riskParamsData.aprMonth = data.data.aprMonth
          vm.riskParamsData.aprYear = data.data.aprYear
          vm.riskParamsData.incomeRedoRatio = data.data.incomeRedoRatio
          vm.riskParamsData.consultingFeeApr = data.data.consultingFeeApr
          vm.riskParamsData.manageFeeApr = data.data.manageFeeApr
          vm.riskParamsData.incomeLiabilitiesRatio = data.data.incomeLiabilitiesRatio
          vm.riskParamsData.accountAmount = data.data.accountAmount
          vm.riskParamsData.MinLimit = data.data.MinLimit
          vm.riskParamsData.MaxLimit = data.data.MaxLimit
          this.$Message.success(message)
          // vm.changeInput()
        } else {
          this.$Message.warning({
            content: message,
            duration: 5,
            closable: true
          })
        }
      }).catch(er => {

      })
    },
    saveDate () {
      var vm = this
      var patternType = vm.riskParamsData.patternType
      var act = vm.riskParamsData.approvedAmount
      var fundSourceCode = vm.riskParamsData.fundSourceCode
      var maxLimit = vm.riskParamsData.maxLimit
      var minLimit = vm.riskParamsData.minLimit
      var debtRate = vm.riskParamsData.incomeLiabilitiesRatio
      var radioDebt = vm.riskParamsData.incomeRedoRatio
      var applyAmount = vm.riskParamsData.applyAmount
      var amt = vm.riskParamsData.applyAmount // 申请金额
      var amtAct = vm.riskParamsData.approvedAmount // 实际借款金额
      var terms = vm.riskParamsData.loanTerms
      var productType = vm.riskParamsData.productType
      var intPriAmact = vm.riskParamsData.approvedAmount
      if (productType === 6) {
        if (terms === 6) {
          if (amtAct > intPriAmact * 1.3) { // 提额不能超过30%
            this.$Message.warning('提额范围不能超过30%,请重新输入!')
            return
          } else if (amtAct < intPriAmact * 0.5) {
            this.$Message.warning('降额范围不能超过50%,请重新输入!')
            return
          } else if (amtAct < 4000 || amtAct > 20000) {
            this.$Message.warning('提降额后额度必须保持在4000到20000区间,请重新输入!')
            return
          }
        }
        if (terms === 12) {
          if (amtAct > intPriAmact * 1.3) { // 提额不能超过30%
            this.$Message.warning('提额范围不能超过30%,请重新输入!')
            return
          } else if (amtAct < intPriAmact * 0.5) {
            this.$Message.warning('降额范围不能超过50%,请重新输入!')
            return
          } else if (amtAct < 4000 || amtAct > 30000) {
            this.$Message.warning('提降额后额度必须保持在4000到30000区间,请重新输入!')
            return
          }
        }
        if (terms === 18) {
          if (amtAct > intPriAmact * 1.3) { // 提额不能超过30%
            this.$Message.warning('提额范围不能超过30%,请重新输入!')
            return
          } else if (amtAct < intPriAmact * 0.5) {
            this.$Message.warning('降额范围不能超过50%,请重新输入!')
            return
          } else if (amtAct < 4000 || amtAct > 50000) {
            this.$Message.warning('提降额后额度必须保持在4000到50000区间,请重新输入!')
            return
          }
        }
        if (terms === 24) {
          if (amtAct > intPriAmact * 1.3) { // 提额不能超过30%
            this.$Message.warning('提额范围不能超过30%,请重新输入!')
            return
          } else if (amtAct < intPriAmact * 0.5) {
            this.$Message.warning('降额范围不能超过50%,请重新输入!')
            return
          } else if (amtAct < 4000 || amtAct > 50000) {
            this.$Message.warning('提降额后额度必须保持在4000到50000区间,请重新输入!')
            return
          }
        }
      }
      if (productType === 2) {
        if (amtAct > intPriAmact * 1.3) { // 提额不能超过30%
          this.$Message.warning('提额范围不能超过30%,请重新输入!')
          return
        }
        if (amtAct < intPriAmact * 0.7) {
          this.$Message.warning('降额范围不能超过30%,请重新输入!')
          return
        }
      }
      if (productType !== 6 && productType !== 2) {
        if (amtAct > 0 && amtAct <= amt) {
          vm.changeInput()
        } else {
          this.$Message.warning('核定金额需大于0且不大于申请金额')
          return
        }
      }

      if (patternType === 'B' && (fundSourceCode === '' || fundSourceCode === null || fundSourceCode === undefined)) {
        this.$Message.warning('资金源不能为空!')
        return
      }
      if (patternType === 'B') {
        for (var i = 1; i < vm.fundSourceList.length; i++) {
          if (vm.fundSourceList[i].fundCode === fundSourceCode) {
            vm.riskParamsData.fundSourceName = vm.fundSourceList[i].fundName
            break
          }
        }
      }
      if (act > applyAmount) {
        this.$Message.warning('核定金额不能大于申请金额!')
        return
      }
      if (debtRate > radioDebt * 100) {
        if (!confirm('负载比超过政策显示，请确定是否需要保存？')) {
          return
        }
      }
      if (debtRate >= 100) {
        if (!confirm('负债比过高, 可能存在风险, 是否继续？')) {
          return
        }
      }
      if (act > maxLimit || act < minLimit) {
        if (!confirm('核定金额不符合当前产品额度区间[' + minLimit + ',' + maxLimit + '], 是否继续?')) {
          return
        }
      }
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/loanapplycase/service/updateRiskInfo', this.riskParamsData).then(re => {
        // var data = re.data
        // var message = data.message
        var data = re.data
        var code = data.code
        var message = data.message
        if (code === 'success') {
          vm.riskParamsData.currentLiabilities = data.data.currentLiabilities
          vm.riskParamsData.incomeLiabilitiesRatio = data.data.incomeLiabilitiesRatio
          vm.riskParamsData.accountAmount = data.data.accountAmount
          vm.riskParamsData.aprMonth = data.data.aprMonth
          vm.riskParamsData.aprYear = data.data.aprYear
          this.$Message.success(message)
        } else {
          this.$Message.warning({
            content: message,
            duration: 5,
            closable: true
          })
        }
      }).catch(er => {

      })
    },
    approvedAmountModify () {

    },
    // calculate(){

    //     if(currDebt != null && currDebt != ""){
    //         if(actDebt != null && actDebt != ""){
    //             if(actIncomes != null && actIncomes != "" ){
    //                 if(actIncomes != '0'){
    //                     var value = accDiv(accAdd(currDebt, actDebt), actIncomes);
    //                     value = Math.round(value*10000)/10000;
    //                     $('#persentIncomesDebt').val((value*100).toFixed(2));
    //                 }else{
    //                     $('#persentIncomesDebt').val("");

    //                 }
    //             }else{
    //                 $('#persentIncomesDebt').val("");
    //                 Ext.Msg.alert('<s:text name="message.alert"/>',"认定收入不为空");
    //             }
    //         }else{
    //             $('#persentIncomesDebt').val("");
    //             Ext.Msg.alert('<s:text name="message.alert"/>',"本笔负债不为空");
    //         }
    //     }else{
    //         $('#persentIncomesDebt').val("");
    //         Ext.Msg.alert('<s:text name="message.alert"/>',"认定负债不为空");
    //     }
    // },
    trialApprovedAmount () {
      var vm = this
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/loanapplycase/service/trialApprovedAmount', this.riskParamsData).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code === 'success') {
          // console.log(data.data.approvedAmount)
          vm.riskParamsData.approvedAmount = data.data.approvedAmount
          vm.riskParamsData.currentLiabilities = data.data.riskParamsDTO.currentLiabilities
          vm.riskParamsData.incomeLiabilitiesRatio = data.data.riskParamsDTO.incomeLiabilitiesRatio
          vm.riskParamsData.accountAmount = data.data.riskParamsDTO.accountAmount
          vm.$Message.success(message)
        } else {
          vm.$Message.warning({
            content: message,
            duration: 5,
            closable: true
          })
        }
      }).catch(er => {

      })
    },
    fundSourceChange (value) {
      if (value === undefined) {
        return
      }
      var vm = this
      this.riskParamsData.fundSourceName = value.label
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/loanapplycase/service/fundChangeTrial', this.riskParamsData).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code === 'success') {
          vm.riskParamsData.aprMonth = data.data.aprMonth
          vm.riskParamsData.aprYear = data.data.aprYear
          // vm.riskParamsData.consultingFeeApr=data.data.consultingFeeApr;
          // vm.riskParamsData.manageFeeApr=data.data.manageFeeApr;
          vm.riskParamsData.incomeRedoRatio = data.data.incomeRedoRatio
          vm.riskParamsData.productCode = data.data.productCode
          vm.riskParamsData.productName = data.data.productName
          // 到账金额, 本笔负债,
          vm.riskParamsData.accountAmount = (vm.riskParamsData.approvedAmount - (vm.riskParamsData.incomeRedoRatio * vm.riskParamsData.approvedAmount)).toFixed(2)
          vm.riskParamsData.currentLiabilities = vm.riskParamsData.currentLiabilities
          vm.riskParamsData.isTrialApproved = 'Y'
          vm.changeInput()
        } else {
          this.$Message.warning({
            content: message,
            duration: 5,
            closable: true
          })
        }
      }).catch(er => {

      })
    },
    changeInput () {
      var vm = this
      // 本笔负债   认定负债  审批认定收入  计算总收入负债比
      var currDebt = vm.riskParamsData.currentLiabilities
      var actDebt = vm.riskParamsData.identificationOfLiabilities
      var actIncomes = vm.riskParamsData.approvalConfirmationIncome
      if (currDebt !== null && actDebt !== null && actIncomes != null && actIncomes !== 0) {
        vm.riskParamsData.incomeLiabilitiesRatio = ''
        vm.riskParamsData.finalTrialed = 'Y'
        let value = this.accDiv(this.accAdd(currDebt, actDebt), actIncomes)
        value = Math.round(value * 10000) / 10000
        vm.riskParamsData.incomeLiabilitiesRatio = (value * 100).toFixed(2)
      } else {
        vm.riskParamsData.incomeLiabilitiesRatio = 0
        vm.riskParamsData.finalTrialed = 'N'
      }
      // if (actIncomes != null && actIncomes !== '' && parseFloat(actIncomes) > 0) {
      //   value = (parseFloat(actDebt) + parseFloat(currDebt)) / parseFloat(actIncomes)
      //   vm.riskParamsData.finalTrialed = 'Y'
      //   vm.riskParamsData.incomeLiabilitiesRatio = (value * 100).toFixed(2)
      // } else {
      //   vm.riskParamsData.incomeLiabilitiesRatio = ''
      //   vm.riskParamsData.finalTrialed = 'N'
      // }
    },
    accAdd (arg1, arg2) {
      let r1
      let r2
      let m
      try {
        r1 = arg1.toString().split('.')[1].length
      } catch (e) {
        r1 = 0
      }
      try {
        r2 = arg2.toString().split('.')[1].length
      } catch (e) {
        r2 = 0
      }
      let maxVal = Math.max(r1, r2)
      m = Math.pow(10, maxVal)
      // console.log(arg1+"@@@@@@@@@"+arg2+"@@@@@@@@@"+m);
      return (arg1 * m + arg2 * m) / m
    },
    accDiv (arg1, arg2) {
      let t1 = 0
      let t2 = 0
      let r1
      let r2
      try {
        t1 = arg1.toString().split('.')[1].length
      } catch (e) {
      }
      try {
        t2 = arg2.toString().split('.')[1].length
      } catch (e) {
      }
      r1 = Number(arg1.toString().replace('.', ''))
      r2 = Number(arg2.toString().replace('.', ''))
      // console.log(r1+"---------"+r2+"----------"+((r1 / r2) * pow(10, t2 - t1)));
      return (r1 / r2) * Math.pow(10, t2 - t1)
    }
  },
  computed: {

  },
  mounted () {
  },
  created () {
    this.$ajax = this.$util.ajax()
  },
  watch: {
    // 点击tab页签，加载数据，只加载一次
    'taskParams.riskParams' (v) {
      if (v) {
        this.initpage()
      }
    }
  },
  props: {
    taskParams: Object
  }
}
</script>
